package logic

import (
	"context"
	"errors"
	"house/model/mysql"
	"house/pkg"

	"house/house"
	"house/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AutherRealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAutherRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AutherRealNameLogic {
	return &AutherRealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AutherRealNameLogic) AutherRealName(in *house.AutherRealNameRequest) (*house.AutherRealNameResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var a mysql.AutherRealName
	a.UserId = in.UserId
	if err = a.GetByUserId(); err != nil {
		logx.Error("查询用户失败")
		return nil, errors.New("查询用户失败")
	}
	if a.ID > 0 {
		logx.Error("用户已实名")
		return nil, errors.New("用户已实名")
	}
	if !pkg.AutherRealName(in.IdCart, in.RealName) {
		logx.Error("调用第三方失败")
		return nil, errors.New("调用第三方失败")
	}
	a.RealName = in.RealName
	a.IdCart = in.IdCart
	if err = a.Create(); err != nil {
		logx.Error("实名失败")
		return nil, errors.New("实名失败")
	}
	return &house.AutherRealNameResponse{
		Id: int64(a.ID),
	}, nil
}
